package yfdc.douyin.download;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;

import kotlin.jvm.internal.Intrinsics;
import okhttp3.OkHttpClient;
import okhttp3.Response;
import yfdc.douyin.download.network.CookieJarImpl;

public final class Utils {
    private Utils() {
        throw new IllegalStateException("Stub!");
    }

    public static <T extends Throwable> void logException(@Nullable final T ex, @NotNull String tag) {
        Intrinsics.checkParameterIsNotNull(tag, "tag");
        if (ex != null) {
            StackTraceElement[] elements = ex.getStackTrace();
            String logTag = tag.isEmpty() ? "Exception" : tag;
            Log.w(logTag, "exception in thread " + Thread.currentThread().getName() +
                    ":" + ex.getClass().getName() + ":" + ex.getMessage());
            for (final StackTraceElement element : elements) {
                Log.w(logTag, " at " + element);
            }
        }
    }

    public static final Gson GSON = (new GsonBuilder())
            .serializeNulls()
            .disableHtmlEscaping()
            .create();

    @Nullable
    public static JsonObject JsonFromString(@NotNull final String json) {
        Intrinsics.checkParameterIsNotNull(json, "json");
        notNull(json);
        if (json.isEmpty()) {
            return null;
        }
        final com.google.gson.TypeAdapter<JsonElement> adapter =
                com.google.gson.internal.bind.TypeAdapters.JSON_ELEMENT;
        try {
            return notNull((JsonObject) adapter.fromJson(json));
        } catch (Exception any) {
            return null;
        }
    }

    private static volatile OkHttpClient _client = null;

    @NotNull
    public static OkHttpClient getGlobalClient() {
        if (_client == null) {
            synchronized (Utils.class) {
                if (_client == null) {
                    _client = (new OkHttpClient.Builder())
                            .addInterceptor(HttpInterceptor.getInstance())
                            .addNetworkInterceptor(HttpInterceptor.getInstance())
                            .cookieJar(CookieJarImpl.INSTANCE)
                            .build();
                }
            }
        }
        return notNull(_client);
    }

    @NotNull
    public static <T> T notNull(@Nullable final T obj) {
        if (obj == null) {
            throw new IllegalStateException("object requires not null.");
        }
        return obj;
    }

    public static final String USER_AGENT = "Mozilla/5.0 (Linux; Android 11; PECM30)" +
            "AppleWebKit/537.36 (KHTML, like Gecko) " +
            "Chrome/99.0.4844.73 Mobile Safari/537.36";

    public static final class HttpInterceptor implements okhttp3.Interceptor {
        private HttpInterceptor() {
            super();
        }

        private static volatile HttpInterceptor _instance = null;

        @NotNull
        public static HttpInterceptor getInstance() {
            if (_instance == null) {
                synchronized (HttpInterceptor.class) {
                    if (_instance == null) {
                        _instance = new HttpInterceptor();
                    }
                }
            }
            return notNull(_instance);
        }

        @NotNull
        @Override
        public Response intercept(@NotNull final Chain chain) throws IOException {
            Intrinsics.checkParameterIsNotNull(chain, "chain");
            final okhttp3.Request req = chain.request().newBuilder()
                    .addHeader("User-Agent", Utils.USER_AGENT)
                    .build();
            return chain.proceed(req);
        }
    }
}
